import os,xbmc,xbmcgui,urllib,urllib2,re,sys

scriptdir=os.path.dirname(__file__)

file=open(scriptdir+"\\favourites.xml", "r")
link=file.read()
info=re.compile('url="url=(.+?)", name="name=(.+?)", iconimage="iconimage=(.+?)", duration="duration=(.+?)"').findall(link)
mode=0
file.close()

for i in range(len(info)):
	if 'url='+info[i][0] == sys.argv[2]:
		dialog = xbmcgui.Dialog()
		dialog.ok('Error','That file already exists!')
		mode = 1
		break

if os.path.isfile(scriptdir+'\\favourites.xml') == False:
	dialog = xbmcgui.Dialog()
	dialog.ok('ERROR', 'An error has occured!')
elif mode == 1:
	dialog = xbmcgui.Dialog()
	dialog.ok('Delete!', 'This feature is not availible yet, if you want to','delete you can do it manually in the "favourite.xml"')	
else:			
	a = open(scriptdir+'\\favourites.xml', 'r+')
	a.read()
	a.write('url="'+sys.argv[2]+'", name="'+sys.argv[1]+'", iconimage="'+sys.argv[3]+'", duration="'+sys.argv[4]+'"\n')
	dialog = xbmcgui.Dialog()
	dialog.ok('Success','Congratulations, you have now added this video','to your favourite list!')